package com.shujia.kafka;

import com.google.gson.Gson;
import com.shujia.kafkaTopicModel.WeiboArticle;
import com.shujia.kafkaTopicModel.WeiboUser;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class KafkaConsumerUser {
    public static void main(String[] args) {
        // 指定kafka配置
        Properties props = new Properties();
        // kafka broker地址
        props.put("bootstrap.servers", "master:9092,node1:9092,node2:9092");
        // 消费者组id
        props.put("group.id", "consumer4");
        // 自动偏移量置为最早的
        props.put("auto.offset.reset", "earliest");
        // 一次最大消费数量
        props.put("max.poll.records", "1000");
        // 指定key的序列化方式
        props.put("key.deserializer", StringDeserializer.class.getName());
        // 指定value的序列化方式
        props.put("value.deserializer", StringDeserializer.class.getName());

        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);

        ConsumerRecords<String, String> msgList;

        String topic = "ods_kafka_user";

        List<String> topicList = Arrays.asList(topic);

        consumer.subscribe(topicList);

        int count = 0;

        try {
            while (true) {
                msgList = consumer.poll(1000);
                Gson gson = new Gson();
                for (ConsumerRecord<String, String> record : msgList) {
                    System.out.println(record.key());
                    System.out.println(record.value());
                    WeiboUser user = gson.fromJson(record.value(), WeiboUser.class);


                    count++;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("一共消费了" + count + "条数据");
            consumer.close();
        }
    }


}
